package com.mworld.galaxy.bull.entity;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;


@Component
public class AvatarHelper implements ServletContextAware {

    private static Avatar DEFAULT_AVATAR_USER = new Avatar(0L, "ghost_person_60x60_v1.png", "ghost_person_200x200_v1.png");
    private static Avatar DEFAULT_AVATAR_COMPANY = new Avatar(0L, "ghost_company_60x60_v1.png", "ghost_company_200x200_v1.png");

    private static ServletContext servletContext;
    private static String pathPrefixUser;
    private static String pathPrefixCompany;

    private static AvatarHelper avatarHelperUser;
    private static AvatarHelper avatarHelperCompany;

    private String pathPrefix;
    private Avatar defaultAvatar;

    public AvatarHelper() {
        super();
    }

    public AvatarHelper(String pathPrefix, Avatar defaultAvatar) {
        this.pathPrefix = pathPrefix;
        this.defaultAvatar = defaultAvatar;
    }

    public Avatar getAvatar(Long id) {
        if (id == null) {
            id = 0L;
        }
        Avatar avatar = generateAvatar(id);
        if (!test(avatar)) {
            avatar = defaultAvatar;
        }
        return avatar;
    }

    private boolean test(Avatar avatar) {
        File file = new File(servletContext.getRealPath(pathPrefix + avatar.getImagePath()));
        return file.exists();
    }

    private Avatar generateAvatar(Long id) {
        StringBuffer sb = new StringBuffer();
        sb.append(StringUtils.leftPad(id.toString(), 9, '0'));
        sb.insert(6, '/');
        sb.insert(3, '/');
        String path = sb.toString();
        String avatarImageUrl = path + ".jpg";
        String avatarSmallImageUrl = path + "_small.jpg";
        return new Avatar(id, avatarSmallImageUrl, avatarImageUrl);
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        AvatarHelper.servletContext = servletContext;
    }

    public String getPathPrefixUser() {
        return pathPrefixUser;
    }

    public void setPathPrefixUser(String pathPrefixUser) {
        AvatarHelper.pathPrefixUser = pathPrefixUser;
    }

    public String getPathPrefixCompany() {
        return pathPrefixCompany;
    }

    public void setPathPrefixCompany(String pathPrefixCompany) {
        AvatarHelper.pathPrefixCompany = pathPrefixCompany;
    }

    public static AvatarHelper getUserAvatarHelper() {
        if (avatarHelperUser == null) {
            avatarHelperUser = new AvatarHelper(pathPrefixUser, DEFAULT_AVATAR_USER);
        }
        return avatarHelperUser;
    }

    public static AvatarHelper getCompanyAvatarHelper() {
        if (avatarHelperCompany == null) {
            avatarHelperCompany = new AvatarHelper(pathPrefixCompany, DEFAULT_AVATAR_COMPANY);
        }
        return avatarHelperCompany;
    }
}
